package com.zd.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zd.dao.BaseDao;
import com.zd.dao.BookDao;
import com.zd.entity.Novel2;

public class BookDaoImpl extends BaseDao implements BookDao {

	@Override
	public List<Novel2> selectNovelByType(String typeName) {
		String sql = "\r\n"
				+ "SELECT t1.*,t2.novelTypeName,t3.novelTypeName as novelTypeName2 ,t4.*\r\n"
				+ "from novel t1,noveltype t2,noveltype t3 , writer t4\r\n"
				+ "WHERE t1.novelTypeId1 = t2.novelTypeId \r\n"
				+ "AND t1.writerId = t4.writerId\r\n"
				+ "and t1.novelTypeId2=t3.novelTypeId \r\n"
				+ "and audit=1 \r\n"
				+ "and (t2.novelTypeName = ? or t3.novelTypeName = ?) ORDER BY releasetime DESC  ";
		Object[] params = {typeName,typeName};
		ResultSet result = super.queryResultSet(sql,params);
		List<Novel2> list = new ArrayList<>();
		try {
			while(result.next()) {
				Novel2 novel = new Novel2();
				novel.setNovelId(result.getInt("novelId"));
				novel.setNovelName(result.getString("novelName"));
				novel.setWriterId(result.getInt("writerId"));
				novel.setNovelImgURL(result.getString("novelImgURL"));
				novel.setNovelTypeId(result.getInt("novelTypeId1"));
				novel.setWriterName(result.getString("writerName"));
				novel.setUpdateTime(result.getDate("releaseTime"));
				novel.setNovelTypeName(result.getString("novelTypeName"));
				novel.setNovelTypeName2(result.getString("novelTypeName2"));
				novel.setNovelIntroduction(result.getString("novelIntroduction"));
				list.add(novel);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}finally {
			super.closeAll(conn, ps, result);
		}
		return null;
	}

	@Override
	public List<Novel2> selectli(String typeName) {
		// TODO Auto-generated method stub
		String sql = "SELECT t1.novelName,t2.novelTypeName,t1.novelId ,t3.novelTypeName as novelTypeName2 \r\n"
				+ "from novel t1,noveltype t2,noveltype t3 \r\n"
				+ "WHERE t1.novelTypeId1 = t2.novelTypeId \r\n"
				+ "and t1.novelTypeId2=t3.novelTypeId \r\n"
				+ "and audit=1 \r\n"
				+ "and (t2.novelTypeName = ? or t3.novelTypeName = ?) ORDER BY releasetime DESC ";
		Object[] params = {typeName,typeName};
		ResultSet result = super.queryResultSet(sql,params);
		List<Novel2> list = new ArrayList<>();
		try {
			while(result.next()) {
				Novel2 novel = new Novel2();
				novel.setNovelId(result.getInt("novelId"));
				novel.setNovelName(result.getString("novelName"));
				novel.setNovelTypeName(result.getString("novelTypeName"));
				novel.setNovelTypeName2(result.getString("novelTypeName2"));
				list.add(novel);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}finally {
			super.closeAll(conn, ps, result);
		}
		return null;
	}

	@Override
	public List<Novel2> selectdiv(String typeName) {
		// TODO Auto-generated method stub
		String sql = "SELECT t1.novelName,t1.novelImgURL,t1.novelId from novel t1,noveltype t2 WHERE t1.novelTypeId1 = t2.novelTypeId and novelTypeName = ? ORDER BY releasetime DESC LIMIT 0,2";
		Object[] params = {typeName};
		ResultSet result = super.queryResultSet(sql,params);
		List<Novel2> list = new ArrayList<>();
		try {
			while(result.next()) {
				Novel2 novel = new Novel2();
				novel.setNovelId(result.getInt("novelId"));
				novel.setNovelName(result.getString("novelName"));
				novel.setNovelImgURL(result.getString("novelImgURL"));
				list.add(novel);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}finally {
			super.closeAll(conn, ps, result);
		}
		return null;
	}

}
